package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import gs.*;
	import gs.easing.*;
	import gs.events.TweenEvent;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.tree.AbstractLayoutCache;
	public class FrameTrade extends JFrame
	{
		//members define
		private var panelOther:JPanel;
		private var labelNameOther:JLabel;
		private var panelItemsOther:JPanel;
		private var labelMoneyOther:JLabel;
		private var panelSelf:JPanel;
		private var labelNameSelf:JLabel;
		private var panelItemsSelf:JPanel;
		private var txtMoneySelf:JTextField;
		private var labelMoneySelf:JLabel;
		private var panel21:JPanel;
		private var btn_trade_prepare:JButton;
		private var btn_trade_trade:JButton;
		private var btn_trade_cancel:JButton;
		public var labelMessage:JLabel;
		//----------------------------------
		private var btn_close:JButton;
		private static var _frame:FrameTrade = null;
		private var _itemsOther:Array = new Array;
		private var _itemsSelf:Array = new Array;
		//private var _tooltipsItemOther:Array = new Array;
		//private var _tooltipsItemSelf:Array = new Array;
		public function FrameTrade()
		{
			setClosable(false);
			setResizable(false);  
			setBorder(new EmptyBorder());
			this.setClipMasked(false);
			getContentPane().setClipMasked(false);
			getTitleBar().getFrame().setClipMasked(false);
			//var filter:GlowFilter;
			//filter = new GlowFilter(0x000000, 1, 2, 2, 100);
			
			//component creation
			setSize(new IntDimension(388, 192));
			setPreferredSize(new IntDimension(388, 192));
			var layout0:EmptyLayout = new EmptyLayout();
			getContentPane().setLayout(layout0);
			
			panelOther = new JPanel();
			panelOther.setLocation(new IntPoint(0, 0));
			panelOther.setSize(new IntDimension(194, 150));
			panelOther.setPreferredSize(new IntDimension(194, 150));
			var layout1:EmptyLayout = new EmptyLayout();
			panelOther.setLayout(layout1);
			
			labelNameOther = new JLabel();
			labelNameOther.setLocation(new IntPoint(55, 7));
			labelNameOther.setSize(new IntDimension(137, 14));
			labelNameOther.setPreferredSize(new IntDimension(137, 14));
			labelNameOther.setText("label");
			
			panelItemsOther = new JPanel();
			panelItemsOther.setLocation(new IntPoint(11, 32));
			panelItemsOther.setSize(new IntDimension(180, 90));
			panelItemsOther.setPreferredSize(new IntDimension(180, 90));
			var layout2:GridLayout = new GridLayout();
			layout2.setRows(2);
			layout2.setColumns(4);
			layout2.setHgap(0);
			layout2.setVgap(0);
			panelItemsOther.setLayout(layout2);
			
			labelMoneyOther = new JLabel();
			labelMoneyOther.setLocation(new IntPoint(42, 133));
			labelMoneyOther.setSize(new IntDimension(53, 14));
			labelMoneyOther.setPreferredSize(new IntDimension(53, 14));
			labelMoneyOther.setText("label");
			
			panelSelf = new JPanel();
			panelSelf.setLocation(new IntPoint(194, 0));
			panelSelf.setSize(new IntDimension(194, 150));
			panelSelf.setPreferredSize(new IntDimension(194, 150));
			var layout3:EmptyLayout = new EmptyLayout();
			panelSelf.setLayout(layout3);
			
			labelNameSelf = new JLabel();
			labelNameSelf.setLocation(new IntPoint(43, 7));
			labelNameSelf.setSize(new IntDimension(137, 14));
			labelNameSelf.setPreferredSize(new IntDimension(137, 14));
			labelNameSelf.setText("label");
			
			panelItemsSelf = new JPanel();
			panelItemsSelf.setLocation(new IntPoint(5, 32));
			panelItemsSelf.setSize(new IntDimension(180, 90));
			panelItemsSelf.setPreferredSize(new IntDimension(180, 90));
			var layout4:GridLayout = new GridLayout();
			layout4.setRows(2);
			layout4.setColumns(4);
			layout4.setHgap(0);
			layout4.setVgap(0);
			panelItemsSelf.setLayout(layout4);
			
			txtMoneySelf = new JTextField();
			txtMoneySelf.setLocation(new IntPoint(25, 130));
			txtMoneySelf.setSize(new IntDimension(53, 20));
			txtMoneySelf.setPreferredSize(new IntDimension(53, 20));
			
			labelMoneySelf = new JLabel();
			labelMoneySelf.setLocation(new IntPoint(128, 133));
			labelMoneySelf.setSize(new IntDimension(53, 14));
			labelMoneySelf.setPreferredSize(new IntDimension(53, 14));
			labelMoneySelf.setText("label");
			
			panel21 = new JPanel();
			panel21.setLocation(new IntPoint(203, 154));
			panel21.setSize(new IntDimension(175, 24));
			panel21.setPreferredSize(new IntDimension(175, 24));
			var layout5:FlowWrapLayout = new FlowWrapLayout();
			layout5.setPreferWidth(200);
			layout5.setAlignment(AsWingConstants.CENTER);
			layout5.setHgap(5);
			layout5.setVgap(0);
			layout5.setMargin(false);
			panel21.setLayout(layout5);
			
			btn_trade_prepare = new JButton();
			btn_trade_prepare.setLocation(new IntPoint(181, 14));
			btn_trade_prepare.setSize(new IntDimension(49, 24));
			btn_trade_prepare.setPreferredSize(new IntDimension(55, 24));
			btn_trade_prepare.setText("准备");
			
			btn_trade_trade = new JButton();
			btn_trade_trade.setLocation(new IntPoint(244, 14));
			btn_trade_trade.setSize(new IntDimension(55, 24));
			btn_trade_trade.setPreferredSize(new IntDimension(55, 24));
			btn_trade_trade.setText("交易");
			
			btn_trade_cancel = new JButton();
			btn_trade_cancel.setLocation(new IntPoint(313, 12));
			btn_trade_cancel.setSize(new IntDimension(55, 24));
			btn_trade_cancel.setPreferredSize(new IntDimension(55, 24));
			btn_trade_cancel.setText("取消");
			
			labelMessage = new JLabel();
			labelMessage.setLocation(new IntPoint(10, 154));
			labelMessage.setSize(new IntDimension(175, 24));
			labelMessage.setPreferredSize(new IntDimension(175, 24));
			labelMessage.setText("label");
			
			//component layoution
			getContentPane().append(panelOther);
			getContentPane().append(panelSelf);
			getContentPane().append(panel21);
			getContentPane().append(labelMessage);
			
			panelOther.append(labelNameOther);
			panelOther.append(panelItemsOther);
			panelOther.append(labelMoneyOther);
			
			panelSelf.append(labelNameSelf);
			panelSelf.append(panelItemsSelf);
			panelSelf.append(txtMoneySelf);
			panelSelf.append(labelMoneySelf);
			
			panel21.append(btn_trade_prepare);
			panel21.append(btn_trade_trade);
			panel21.append(btn_trade_cancel);
			
			//------------------------------------------------
			btn_close = new JButton();
			btn_close.setLocation(new IntPoint(82, 0));
			btn_close.setSize(new IntDimension(27, 27));
			btn_close.setPreferredSize(new IntDimension(27, 27));
			this.getTitleBar().setCloseButton(btn_close);
			setImages();
			addEventListener(FrameEvent.FRAME_CLOSING,onFrameClosing);
			addEventListener(AWEvent.HIDDEN,onFrameClosing);
		}
		private function onFrameClosing(e:Event):void
		{
			if(FrameBag.isShowing())
				FrameBag.hide();
			if(!_trade_success)
				Client.getInstance().sendPacketLogic(806,[0]);
		}
		
		public function setImages():void
		{
			var img_background:DisplayObject = Client.getUIElement("img_trade_bg") as DisplayObject
			setBackgroundDecorator(new AssetBackground(img_background));
			setSize(new IntDimension(img_background.width, img_background.height));
			setPreferredSize(new IntDimension(img_background.width, img_background.height));
			setSizeWH(img_background.width,img_background.height);
			btn_close.wrapSimpleButton(Client.getUIElement("btn_close") as SimpleButton);
			pack();
			setLocationXY((Engine.W - width)/2,(Engine.H - height)/2);
			
			btn_trade_prepare.wrapSimpleButton(Client.getUIElement("btn_trade_prepare") as SimpleButton);
			btn_trade_trade.wrapSimpleButton(Client.getUIElement("btn_trade_trade") as SimpleButton);
			btn_trade_cancel.wrapSimpleButton(Client.getUIElement("btn_trade_cancel") as SimpleButton);
			btn_trade_prepare.setText("");
			btn_trade_trade.setText("");
			btn_trade_cancel.setText("");
			btn_trade_prepare.addActionListener(onBtnPrepare);
			btn_trade_trade.addActionListener(onBtnTrade);
			btn_trade_cancel.addActionListener(onBtnCancel);
			
			var item:PanelItem;
			var nIndex:uint = 0;
			var i:int = 0;
			for(i = 0; i < TRADE_ITEM_COUNT; i++)
			{
				item = new PanelItem;
				_itemsOther.push(item);
				panelItemsOther.append(item);
				item.pack();
				item.paintImmediately();
				
				item.addEventListener(ClickCountEvent.CLICK_COUNT, onItemClickCount);  
				item.setDragEnabled(true);
				item.addEventListener(DragAndDropEvent.DRAG_RECOGNIZED, onDragRecognized);
				
				item.setDropTrigger(true);
				item.setDragAcceptableInitiatorAppraiser(onDropAppraiser);
				item.addEventListener(DragAndDropEvent.DRAG_ENTER, onDragEnter);
				item.addEventListener(DragAndDropEvent.DRAG_EXIT, onDragExit);
				item.addEventListener(DragAndDropEvent.DRAG_DROP, onDragDrop);
				
				//item.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
				//item.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
				item.setBorder(new EmptyBorder());
				item.index = nIndex++;
				item.enableClick = false;
				arItemTradeOther[i] = {csv_id:0}
				
			}

			nIndex = 0;
			
			for(i = 0; i < TRADE_ITEM_COUNT; i++)
			{
				item = new PanelItem;
				_itemsSelf.push(item);
				panelItemsSelf.append(item);
				item.pack();
				item.paintImmediately();
				
				item.addEventListener(ClickCountEvent.CLICK_COUNT, onItemClickCount);  
				item.setDragEnabled(true);
				item.addEventListener(DragAndDropEvent.DRAG_RECOGNIZED, onDragRecognized);
				
				item.setDropTrigger(true);
				item.setDragAcceptableInitiatorAppraiser(onDropAppraiser);
				item.addEventListener(DragAndDropEvent.DRAG_ENTER, onDragEnter);
				item.addEventListener(DragAndDropEvent.DRAG_EXIT, onDragExit);
				item.addEventListener(DragAndDropEvent.DRAG_DROP, onDragDrop);
				
				//item.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
				//item.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
				item.setBorder(new EmptyBorder());
				item.index = nIndex++;
				_dropItems[i] = {page:-1,index:-1};
				arItemTradeSelf[i] = {csv_id:0}
			}
			
			var filter:GlowFilter;
			var font:ASFont = new ASFont("宋体", 14, false, false, false, false);
			filter = new GlowFilter(0x000000, 1, 2, 2, 10);
			
			labelNameOther.setFont(font);
			labelNameOther.setTextFilters(new Array(filter));
			
			labelNameSelf.setFont(font);
			labelNameSelf.setTextFilters(new Array(filter));	
			
			font = new ASFont("宋体", 12, false, false, false, false);
			txtMoneySelf.setBackgroundDecorator(null);
			txtMoneySelf.setFont(font);
			txtMoneySelf.filters = new Array(filter);
			txtMoneySelf.setForeground(ASColor.ORANGE);
			txtMoneySelf.setBorder(new EmptyBorder);
			
			labelMoneyOther.setFont(font);
			labelMoneyOther.setTextFilters(new Array(filter));	
			labelMoneySelf.setFont(font);
			labelMoneySelf.setTextFilters(new Array(filter));	
			
			labelMessage.setFont(font);
			labelMessage.setForeground(ASColor.RED);
			labelMessage.setTextFilters(new Array(filter));

			labelNameOther.setForeground(ASColor.WHITE);
			labelNameSelf.setForeground(ASColor.WHITE);
			labelNameSelf.setText(CharInfo.charName);
			labelMoneySelf.setText(CharInfo.money.toString());
			labelMoneyOther.setForeground(ASColor.ORANGE);
			labelMoneySelf.setForeground(ASColor.ORANGE);
			
			labelMessage.setText("放入物品->准备->交易");
			//updateMessage();
			this.paintImmediately();
		}
		
		protected function onBtnPrepare(e:Event):void
		{
			var array:Array = new Array();
			array.push(int(txtMoneySelf.getText()));
			var i:int = 0;
			array.push(TRADE_ITEM_COUNT);
			for(i = 0; i < TRADE_ITEM_COUNT; i++)
			{
				array.push(i);
				array.push(_dropItems[i].page);
				array.push(_dropItems[i].index);
			}
			Client.getInstance().sendPacketLogic(803,array);
		}
		protected function onBtnTrade(e:Event):void
		{
			Client.getInstance().sendPacketLogic(804,[0]);
		}
		protected function onBtnCancel(e:Event):void
		{
			Client.getInstance().sendPacketLogic(805,[0]);
		}
		
		private function onRollOver(event:MouseEvent):void
		{
			var target:PanelItem = event.target as PanelItem;
			if(!(target.getBorder() is EmptyBorder))
				return;
			target.setBorder(new LineBorder(null,ASColor.GREEN));
		}
		
		private function onRollOut(event:MouseEvent):void
		{
			var target:PanelItem = event.target as PanelItem;
			target.setBorder(new EmptyBorder());
		}
		
		//是否接受这个被拖入的组件
		private function onDropAppraiser(initiator:JPanel):Boolean
		{
			if(FrameBag.isShowing())
				return FrameBag._frame._items.indexOf(initiator) != -1;
			return false;//_itemsEquip.indexOf(initiator) != -1 || _items.indexOf(initiator) != -1;
		}
		//被接受的组件被拖入时，改变Panel背景色
		private function onDragEnter(e:DragAndDropEvent):void
		{
			var initiator:PanelItem = e.getDragInitiator() as PanelItem;
			if(onDropAppraiser(initiator))
			{
				if(e.target is PanelItem)
				{
					var target:PanelItem = e.target as PanelItem;
					target.setBorder(new LineBorder(null,ASColor.GREEN));
				}
				
				if(e.target is JToggleButton)
				{
					var btn:JToggleButton = e.target as JToggleButton;
					btn.setBorder(new LineBorder(null,ASColor.GREEN));
				}
			}
		}
		
		//组件被拖出时，恢复原来的背景色
		private function onDragExit(e:DragAndDropEvent):void
		{
			if(e.target is PanelItem)
			{
				var target:JPanel = e.target as JPanel;
				target.setBorder(new EmptyBorder());
			}
			if(e.target is JToggleButton)
			{
				var btn:JToggleButton = e.target as JToggleButton;
				btn.setBorder(new EmptyBorder());
			}
		}
		//组件被释放时，如果是被接受的，那么把它加入此面板
		private var _dropItems:Array = new Array();
		private function onDragDrop(event:DragAndDropEvent):void
		{
			var initiator:PanelItem = event.getDragInitiator() as PanelItem;
			if(event.target is PanelItem)
			{
				var target:PanelItem = event.target as PanelItem;
				target.setBorder(new EmptyBorder());
				if(onDropAppraiser(initiator))
				{
					//Client.getInstance().sendPacketLogic(17,[_currentPage,initiator.index,_currentPage,target.index]);
					//var objItemCSV:Object = Client.getInstance().GetItemCSV(CharInfo.m_arItemPage[FrameBag._currentPage][initiator.index].csv_id);
					//target.icon_id = objItemCSV.icon_id;
					//updateIcon(target);
					for each(var item:Object in _dropItems)
					{
						if(item.page == FrameBag._currentPage && item.index == initiator.index)
							return;
					}
					_dropItems[target.index] = {page:FrameBag._currentPage,index:initiator.index};
					arItemTradeSelf[target.index] = CharInfo.m_arItemPage[FrameBag._currentPage][initiator.index];
					updateItems();
				}
			}
		}
		/**
		 * 监听到dragBtn的ON_DRAG_RECOGNIZED事件，将拖拽事件托管给DragManager。
		 * @param dragInitiator          此例中是指dragBtn。
		 * @param touchedChild    被鼠标拖住的组件，此例中亦是dragBtn。
		 */
		
		//private var _currentIndex:uint = 0;
		private function onDragRecognized( e:DragAndDropEvent):void
		{
			var btn:PanelItem = e.target as PanelItem;
			if(btn.icon_id <= 0)
				return;
			DragManager.startDrag(btn,new SourceData("item", btn.getToolTipText()),new DraggingImageItem(btn)); 
			//_currentIndex = btn.index;
		}
		
		private function onItemClickCount(event:ClickCountEvent):void
		{   
			
			if(event.getCount() >= 2)
			{   
				
				var target:PanelItem = event.target as PanelItem;
				target.setBorder(new EmptyBorder());

				_dropItems[target.index] = {page:-1,index:-1};
				arItemTradeSelf[target.index] = {csv_id:0};
				updateItems();
				Client.getInstance().sendPacketLogic(805,[0]);
			}   
			
		} 
		
		public static function show():void
		{
			
			if(isShowing())
			{
				hide();
				return;
			}
			if(_frame == null)
			{
				_frame = new FrameTrade();
				PanelMainHelp.getInstance().addChild(_frame);
			}
			_frame.show();
			PanelMainHelp.getInstance().bringToTop(_frame);
			
			_frame._otherPrepared = false;
			_frame._selfPrepared = false;
			_frame._otherTraded = false;
			_frame._selfTraded = false;
			updateMessage();
			var i:int = 0;
			for(i = 0; i < TRADE_ITEM_COUNT; i++)
			{
				_frame.arItemTradeOther[i] = {csv_id:0};
				_frame.arItemTradeSelf[i] = {csv_id:0};
			}
			_frame.updateItems();
			if(FrameBag.isShowing())
				FrameBag.hide();
			FrameBag.show();
			_frame.labelMoneySelf.setText(CharInfo.money.toString());
			_frame.panelOther.setBorder(new EmptyBorder);
			_frame.panelSelf.setBorder(new EmptyBorder);
			
			var _frameDest:JFrame = FrameBag._frame;
			
			_frame.setLocationXY((Engine.W - _frame.width)/2,(Engine.H - _frame.height)/2);
			_frameDest.setLocationXY((Engine.W - _frameDest.width)/2,(Engine.H - _frameDest.height)/2);
			
			
			var teamDestX:int = (Engine.W - (_frameDest.width+_frame.width))/2;
			var teamDestY:int = (Engine.H - _frame.height)/2;
			
			var listDestX:int = teamDestX + _frame.width;
			var listDestY:int = teamDestY + (_frame.height - _frameDest.height)/2;
			
			var tween:TweenLite = TweenLite.to(_frame, 0.3, {x:teamDestX, y:teamDestY});
			var list_tween:TweenLite = TweenLite.to(_frameDest, 0.3, {x:listDestX, y:listDestY});
			
		}
		public static function hide():void
		{
			if(isShowing())_frame.hide();
		}
		public static function isShowing():Boolean
		{
			return(_frame != null&& _frame.isShowing());
		}
		
		public static function updateMessage():void
		{
			_frame.btn_trade_prepare.setEnabled(true);
			_frame.btn_trade_trade.setEnabled(true);
			_frame.btn_trade_cancel.setEnabled(true);
			
			if(!_frame._otherPrepared || !_frame._selfPrepared)
			{
				_frame._otherTraded = false;
				_frame._selfTraded = false;
			}
			
			if(!_frame._otherPrepared && !_frame._selfPrepared)
			{
				_frame.labelMessage.setText("放入物品->准备->交易");
				_frame.btn_trade_prepare.setEnabled(true);
				_frame.btn_trade_trade.setEnabled(false);
				_frame.btn_trade_cancel.setEnabled(false);
			}
			if(_frame._otherPrepared && !_frame._selfPrepared)
			{
				_frame.labelMessage.setText("对方已准备就绪");
				_frame.btn_trade_prepare.setEnabled(true);
				_frame.btn_trade_trade.setEnabled(false);
				_frame.btn_trade_cancel.setEnabled(false);
			}
			if(!_frame._otherPrepared && _frame._selfPrepared)
			{
				_frame.labelMessage.setText("准备就绪,等待对方准备");
				_frame.btn_trade_prepare.setEnabled(false);
				_frame.btn_trade_trade.setEnabled(false);
				_frame.btn_trade_cancel.setEnabled(true);
			}
			if(_frame._otherPrepared && _frame._selfPrepared)
			{
				_frame.labelMessage.setText("双方准备就绪,请按交易");
				_frame.btn_trade_prepare.setEnabled(false);
				_frame.btn_trade_trade.setEnabled(true);
				_frame.btn_trade_cancel.setEnabled(true);
			}
			
			if(_frame._otherTraded && !_frame._selfTraded)
			{
				_frame.labelMessage.setText("对方已确认交易");
				_frame.btn_trade_prepare.setEnabled(false);
				_frame.btn_trade_trade.setEnabled(true);
				_frame.btn_trade_cancel.setEnabled(true);
			}
			if(!_frame._otherTraded && _frame._selfTraded)
			{
				_frame.labelMessage.setText("已确认交易,等待对方确认交易");
				_frame.btn_trade_prepare.setEnabled(false);
				_frame.btn_trade_trade.setEnabled(false);
				_frame.btn_trade_cancel.setEnabled(true);
			}
		}
		public static function phTradeInvite_800(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phTradeInvite_800", msg);
			var nIndex:uint = 0;
			var invite_char_id:int = msg[nIndex++];
			var invite_char_name:String = msg[nIndex++];
			
			var pane:FrameInfo = FrameInfo.showMessageDialog("","是否同意 "+invite_char_name+" 的交易请求?",handleOptionTradeInvite,null,true,null,JOptionPane.YES|JOptionPane.NO);
			inviteCharId = invite_char_id;
	
		}
		private static var inviteCharId:uint = 0;
		private static function handleOptionTradeInvite(button:uint):void
		{
			Client.getInstance().sendPacketLogic(801,[inviteCharId,button == FrameInfo.YES?0:1]);
		}
		private static var tradeOtherCharId:int = 0;
		public static function phTradeStart_802(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phTradeStart_802", msg);
			var nIndex:uint = 0;
			var invite_char_id:int = msg[nIndex++];
			var invite_char_name:String = msg[nIndex++];
			var invited_char_id:int = msg[nIndex++];
			var invited_char_name:String = msg[nIndex++];
			tradeOtherCharId = invite_char_id;
			if(tradeOtherCharId == CharInfo.charId)
			{
				tradeOtherCharId = invited_char_id;
			}
			show();
			_trade_success = false;
			
			_frame.labelNameOther.setText(invite_char_name);
			if(invite_char_id == CharInfo.charId)
			{
				_frame.labelNameOther.setText(invited_char_name);
			}
		}
		
		private var arItemTradeOther:Array = new Array;
		private var arItemTradeSelf:Array = new Array;
		private var _otherPrepared:Boolean = false;
		private var _selfPrepared:Boolean = false;
		private var _otherTraded:Boolean = false;
		private var _selfTraded:Boolean = false;
		public static function phTradePrepare_803(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phTradePrepare_803", msg);
			var nIndex:uint = 0;
			var m_nCharId:int = msg[nIndex++];
			if(tradeOtherCharId == m_nCharId)
			{
				_frame.panelOther.setBorder(new LineBorder(null,ASColor.GREEN));
				_frame._otherPrepared = true;
			}
			else
			{
				_frame.panelSelf.setBorder(new LineBorder(null,ASColor.GREEN));
				_frame._selfPrepared = true;
			}
			
			if(tradeOtherCharId == m_nCharId)
			{
				var money:int = msg[nIndex++];
				_frame.labelMoneyOther.setText(money.toString());
				var array:Array = _frame.arItemTradeOther;
				array.splice(0,array.length);
				var nCount:uint = msg[nIndex++];
				for(var i:int = 0; i < nCount; i++)
				{
					var obj:Object = new Object();
					array[i] = obj;
					obj.csv_id = msg[nIndex++];
					if(obj.csv_id == 0)
					{
						continue;
					}
					
					obj.count = msg[nIndex++];
					obj.equip_level = msg[nIndex++];
					obj.equip_hp = msg[nIndex++];
					obj.equip_ap = msg[nIndex++];
					obj.equip_dp = msg[nIndex++];
					obj.equip_sp = msg[nIndex++];
					
					obj.equip_enhanceHL = msg[nIndex++];
					obj.equip_enhanceBD = msg[nIndex++];
					obj.equip_enhanceHS = msg[nIndex++];
					
					obj.equip_resistHL = msg[nIndex++];
					obj.equip_resistBD = msg[nIndex++];
					obj.equip_resistHS = msg[nIndex++];
					
					obj.equip_prop_BJ = msg[nIndex++];
					obj.equip_prop_SB = msg[nIndex++];
					obj.equip_prop_FJ = msg[nIndex++];
					obj.equip_prop_LJ = msg[nIndex++];
					
					obj.creator_char_gender = msg[nIndex++];
					obj.creator_char_name = msg[nIndex++];
					obj.creator_lover_gender = msg[nIndex++];
					obj.creator_lover_name = msg[nIndex++];
					
				}
				_frame.updateItems();
			}
			
			updateMessage();
		}
		public function updateItems():void
		{
			var nIndex:uint = 0;
			var item:Object = null;
			var objItemCSV:Object = null;
			for each(item in arItemTradeOther)
			{
				/*if(item.csv_id != 0)
				{
					objItemCSV = Client.getInstance().GetItemCSV(item.csv_id);
					_itemsOther[nIndex].icon_id = objItemCSV.icon_id;
					
					if(_tooltipsItemOther[nIndex] == null)
						_tooltipsItemOther[nIndex] = new ToolTipItem(item);
					else
						_tooltipsItemOther[nIndex].setItem(item);
					_tooltipsItemOther[nIndex].setTargetComponent(_itemsOther[nIndex]);
				}
				else
				{
					_itemsOther[nIndex].icon_id = 0;
					if(_tooltipsItemOther[nIndex] != null)
						_tooltipsItemOther[nIndex].setTargetComponent(null);
				}*/
				//trace("setData,_items[nIndex].icon_id:"+_items[nIndex].icon_id);
				//updateIcon(_itemsOther[nIndex]);
				
				_itemsOther[nIndex].item = item;
				nIndex++;
				if(nIndex >= TRADE_ITEM_COUNT)
					break;
			}
			
			nIndex = 0;
			for each(item in arItemTradeSelf)
			{
				/*if(item.csv_id != 0)
				{
					objItemCSV = Client.getInstance().GetItemCSV(item.csv_id);
					_itemsSelf[nIndex].icon_id = objItemCSV.icon_id;
					
					if(_tooltipsItemSelf[nIndex] == null)
						_tooltipsItemSelf[nIndex] = new ToolTipItem(item);
					else
						_tooltipsItemSelf[nIndex].setItem(item);
					_tooltipsItemSelf[nIndex].setTargetComponent(_itemsSelf[nIndex]);
				}
				else
				{
					_itemsSelf[nIndex].icon_id = 0;
					if(_tooltipsItemSelf[nIndex] != null)
						_tooltipsItemSelf[nIndex].setTargetComponent(null);
				}
				updateIcon(_itemsSelf[nIndex]);*/
				_itemsSelf[nIndex].item = item;
				nIndex++;
				if(nIndex >= TRADE_ITEM_COUNT)
					break;
			}
		}
		public static var _trade_success:Boolean = false;
		public static function phTradeTrade_804(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phTradeTrade_804", msg);
			var nIndex:uint = 0;
			var result:int = msg[nIndex++];
			if(result == 0)
			{
				_trade_success = true;
				hide();
			}
			else
			{
				var m_nCharId:int = msg[nIndex++];
				if(tradeOtherCharId == m_nCharId)
				{
					_frame._otherTraded = true;
				}
				else
				{
					_frame._selfTraded = true;
				}
				updateMessage();
			}
		}
		public static var TRADE_ITEM_COUNT:int = 8;
		public static function phTradeCancel_805(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phTradeCancel_805", msg);
			var nIndex:uint = 0;
			var m_nCharId:int = msg[nIndex++];
			if(tradeOtherCharId == m_nCharId)
			{
				_frame.panelOther.setBorder(new EmptyBorder);
				var i:int = 0;
				for(i = 0; i < TRADE_ITEM_COUNT; i++)
				{
					_frame.arItemTradeOther[i] = {csv_id:0}
					
				}
				_frame.labelMoneyOther.setText("");
				_frame.updateItems();
				_frame._otherPrepared = false;
			}
			else
			{
				_frame.panelSelf.setBorder(new EmptyBorder);
				_frame._selfPrepared = false;
			}
			_frame._otherTraded = false;
			_frame._selfTraded = false;
			updateMessage();
		}
		public static function phTradeClose_806(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phTradeClose_806", msg);
			var nIndex:uint = 0;
			//var m_nCharId:int = msg[nIndex++];
			hide();
		}
	
		

	}
}